package com.myres.service.place;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myres.db.place.PlaceAccessor;
import com.myres.model.place.Zone;
import com.myres.service.ServiceServlet;

public class AddZone extends ServiceServlet {
	private PlaceAccessor accessor;
	/**
	 * Constructor of the object.
	 */
	public AddZone() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		try{
			accessor.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void init() throws ServletException {
		// Put your code here
		accessor=new PlaceAccessor();
	}

	@Override
	public void doRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("requested");
		Zone zone=new Zone();
		String param=request.getParameter("zone");
		if(param==null||param.length()<3){
			response.setStatus(505);
			return;
		}
		zone.setZone(param);
		zone.setValue(param);
		try{
			int count=accessor.insertZone(zone);
			if(count==1){
				response.setStatus(200);
				System.out.println("add success");
			}else{
				response.setStatus(505);
				System.out.println("add fail");
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}

}
